<template>
    <div>
        <global-tables
            @changePage="handleChangePage"
            :total="total"
            @handleAllSelect="handleAllSelect"
            :list="list"
            :loading="loading"
            :columns="columns"
            :current="search.pageindex"
        >
            <Form :model="search" :label-width="80">
                <div class="table-top-wrap">
                    <div>
                        <div>
                            <Button v-if="isAddBtn" class="features-btn" @click="handleBtnClick(null,'add')">
                                <Icon type="md-add" size="18" />新建
                            </Button>
                            <Button v-if="isDelete" class="features-btn btn-auto-m" @click="handleBtnClick(null,'delete')">
                                <Icon type="md-trash" size="17" />删除
                            </Button>
                        </div>
                    </div>
                    <div class="table-top-wrap-bottom">
                        <div>
                            <input type="text" class="style-input" @keyup.enter="handleSearch" v-model="search.keyword" placeholder="请输入信息" />
                            <i-input style="display:none"></i-input>
                        </div>
                        <div>
                            <Button icon="ios-search" @click="handleSearch" class="search-btn">搜索</Button>
                        </div>
                    </div>
                </div>
            </Form>
        </global-tables>
        <Modal :mask-closable="false" v-model="isModal" :title="ModalTitle" width="800">
            <div v-if="isModal">
                <Form ref="formItem" :model="formItem" :rules="ruleValidate" :label-width="80">
                    <Row>
                        <i-col :lg="{ span: 23}">
                            <FormItem label="标题名称" prop="InfoName">
                                <i-input v-model="formItem.InfoName" placeholder="请输入新闻名称"></i-input>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 23}">
                            <FormItem label="内容">
                                <editor ref="editor" :value="content" @on-change="handleChange" />
                            </FormItem>
                        </i-col>
                    </Row>
                </Form>
            </div>
            <div slot="footer">
                <Button size="large" @click="handleClose">取消</Button>
                <Button type="primary" size="large" @click="handleSubmit('formItem')">确定</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
/**
 * 信息列表
 */
import { cyclicRouting, getdate } from '@/util';
import { getModulePermission } from '@/api/user';
import { getTablesList, addTablesItem, deleteItem, getItemInfo } from './service';
import Editor from '@/components/Editor';
import GlobalTables from '@/components/GlobalTables';

export default {
    name: 'OrganizationShop',
    components: {
        Editor,
        GlobalTables
    },
    data() {
        return {
            allSelectArr: null,
            isAddBtn: false,
            isProhibit: false,
            isEdit: false,
            isModal: false,
            isDelete: false,
            ModalTitle: '新建信息',
            ntypeList: [],
            itemId: 0,
            loading: true,
            formItem: this.initFormItem(),
            ruleValidate: this.initRuleValidate(),
            content: '1111',
            total: 0,
            list: [],
            search: {
                keyword: '',
                pageindex: 1,
                pagesize: 10
            },
            columns: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    title: '标题',
                    key: 'InfoName'
                },
                {
                    title: '创建时间',
                    width: 150,
                    render: (h, params) => {
                        return h('div', [
                            params.row.CreateTime &&
                                h(
                                    'span',
                                    {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        }
                                    },
                                    getdate(new Date(params.row.CreateTime).getTime())
                                )
                        ]);
                    }
                },
                {
                    title: '最后修改时间',
                    width: 150,
                    render: (h, params) => {
                        if (params.row.LastModifyTime == '1970-01-01 00:00:00') {
                            return h('div', [
                                params.row.LastModifyTime &&
                                    h(
                                        'span',
                                        {
                                            props: {
                                                type: 'primary',
                                                size: 'small'
                                            }
                                        },
                                        '未修改'
                                    )
                            ]);
                        } else {
                            return h('div', [
                                params.row.LastModifyTime &&
                                    h(
                                        'span',
                                        {
                                            props: {
                                                type: 'primary',
                                                size: 'small'
                                            }
                                        },
                                        getdate(new Date(params.row.LastModifyTime).getTime())
                                    )
                            ]);
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    width: 100,
                    render: (h, params) => {
                        const { isEdit } = this;

                        return h('div', [
                            isEdit &&
                                h(
                                    'Button',
                                    {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            background: '#5ab1ef',
                                            border: 'none',
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.handleBtnClick(params.row, 'edit');
                                            }
                                        }
                                    },
                                    '修改'
                                )
                        ]);
                    }
                }
            ]
        };
    },
    mounted() {
        /* 获取菜单的id */
        cyclicRouting(JSON.parse(sessionStorage.getItem('user')).menuList, this.$route, id => {
            /* 按钮权限请求 */
            getModulePermission({
                roleid: JSON.parse(sessionStorage.getItem('user')).roleid,
                moduleid: id
            }).then(res => {
                /* 按钮权限赋值 */
                for (let item in res.data) {
                    if (res.data[item].name == 'add') {
                        this.isAddBtn = true;
                    } else if (res.data[item].name == 'edit') {
                        this.isEdit = true;
                    } else if (res.data[item].name == 'delete') {
                        this.isDelete = true;
                    } else if (res.data[item].name == 'edit') {
                        this.isEdit = true;
                    }
                }
                /* 列表请求 */
                this.getTablesListData();
            });
        });
    },
    methods: {
        /* 表格搜索 */
        handleSearch() {
            this.loading = true;
            this.getTablesListData();
        },
        /* 关闭对话框 */
        handleClose() {
            this.isModal = false;
        },
        handleChange(html) {
            this.formItem.Detail = html;
        },
        /* 复选框选中 */
        handleAllSelect(list) {
            let arr = [];
            for (let item in list) {
                arr.push(list[item].ID);
                arr.join(',');
            }
            arr = arr.join(',');
            this.allSelectArr = arr;
        },
        /* 提交 */
        handleSubmit(name) {
            this.$refs[name].validate(valid => {
                if (valid) {
                    let data = this.formItem;
                    addTablesItem(data).then(res => {
                        if (res.state == 'success') {
                            this.$Message.success('操作成功');
                            this.loading = true;
                            this.isModal = false;
                            this.getTablesListData();
                        } else {
                            this.$Message.error(res.message);
                        }
                    });
                }
            });
        },
        /* 表格数据请求 */
        getTablesListData() {
            const data = this.search;
            getTablesList(data).then(res => {
                this.list = res.data.rows;
                this.loading = false;
                this.total = res.data.records;
            });
        },
        /* 表格分页方法 */
        handleChangePage(type, page) {
            this.loading = true;
            if (type == 'page') {
                this.search.pageindex = page;
                this.getTablesListData();
            } else {
                this.search.pagesize = page;
                this.getTablesListData();
            }
        },
        /* 对话框 */
        handleBtnClick(item, type) {
            this.ModalTitle = '新建信息';
            this.formItem = this.initFormItem();
            if (type == 'edit' || type == 'add') {
                this.content = ' ';
                this.formItem.Detail = ' ';
                this.itemId = 0;
                this.isModal = true;
                if (type == 'edit') {
                    this.ModalTitle = '修改信息';

                    getItemInfo({
                        id: item.ID
                    }).then(res => {
                        this.itemId = item.ID;
                        this.formItem = {
                            ...res.data
                        };

                        this.$refs.editor.setHtml(res.data.Detail);
                    });
                }
            } else if (type === 'delete') {
                if (this.allSelectArr) {
                    this.$Modal.confirm({
                        title: `你确定要删除吗`,
                        onOk: () => {
                            deleteItem({
                                ids: this.allSelectArr
                            }).then(res => {
                                if (res.state == 'success') {
                                    this.$Message.success(res.message);
                                    this.allSelectArr = '';
                                } else {
                                    this.$Message.error(res.message);
                                }
                                this.getTablesListData();
                            });
                        },
                        onCancel: () => {
                            this.$Message.info('取消成功');
                        }
                    });
                } else {
                    this.$Message.error('请先选择需要操作的记录');
                }
            }
        },
        /* 表单数据 */
        initFormItem() {
            return {
                InfoName: '',
                Detail: '',
                ID: '0'
            };
        },
        /* 表单校验 */
        initRuleValidate() {
            return {
                InfoName: [
                    {
                        required: true,
                        message: '请输入标题名称',
                        trigger: 'change'
                    }
                ]
            };
        }
    }
};
</script>

<style>
</style>
